/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 StdAsymmetricEncrypter.hpp. Created on: 03.09.2009 11:40:38
 */

#ifndef STDASYMMETRICENCRYPTER_HPP_
#define STDASYMMETRICENCRYPTER_HPP_

#include "smartobjects/SmartObjectsDefs.hpp"
#include "IAsymmetricDataEncrypter.hpp"

#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>


namespace smartobjects {

class StdAsymmetricEncrypter : public IAsymmetricDataEncrypter {
public:
	SO_DLLPRFX StdAsymmetricEncrypter();
	SO_DLLPRFX virtual ~StdAsymmetricEncrypter();

	SO_DLLPRFX virtual void initCipher( PrivateKey* key );
	SO_DLLPRFX virtual void initCipher( PublicKey* key );

	SO_DLLPRFX virtual PublicKey* getPublicKey() ;
	SO_DLLPRFX virtual PrivateKey* getPrivateKey() ;

	SO_DLLPRFX virtual PublicKey* decodePublicKey(ByteBuffer*  poBuffer);
	SO_DLLPRFX virtual void encodePublicKey(PublicKey* key, ByteBuffer*  poBuffer );
	SO_DLLPRFX virtual KeyPair* generateKeyPair();

	SO_DLLPRFX virtual void encrypt(ByteBuffer* poInputData, ByteBuffer* poOutput);
	SO_DLLPRFX virtual void decrypt(ByteBuffer* poEncryptedData, ByteBuffer* poOutput);
	SO_DLLPRFX virtual const std::string& getAlgorithm() const;


protected:

	SO_DLLPRFX static const std::string ALGORITHM_NAME;
	SO_DLLPRFX static const unsigned long KEY_LENGTH;

	RSA* m_pEncrypter;
	RSA* m_pDecrypter;

	boost::recursive_mutex m_mxEncrypter;
	boost::recursive_mutex m_mxDecrypter;

	PublicKey* m_poPublicKey;
	PrivateKey* m_poPrivateKey;

};

}

#endif /* STDASYMMETRICENCRYPTER_HPP_ */
